<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" 
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OA 系统仪表板</title>
    <link rel="stylesheet" th:href="@{/css/common.css}">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .logo {
            display: flex;
            align-items: center;
            font-size: 1.5em;
            font-weight: bold;
        }
        
        .logo-icon {
            font-size: 1.8em;
            margin-right: 10px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .user-avatar {
            width: 40px;
            height: 40px;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2em;
        }
        
        .nav-menu {
            background: white;
            border-bottom: 1px solid #e1e5f7;
            padding: 0 2rem;
        }
        
        .nav-content {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            gap: 30px;
        }
        
        .nav-item {
            padding: 15px 0;
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            border-bottom: 3px solid transparent;
            transition: all 0.3s ease;
        }
        
        .nav-item:hover, .nav-item.active {
            color: #764ba2;
            border-bottom-color: #764ba2;
        }
        
        .main-content {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        
        .welcome-section {
            background: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
        }
        
        .welcome-title {
            font-size: 2em;
            color: #333;
            margin-bottom: 10px;
        }
        
        .welcome-subtitle {
            color: #666;
            font-size: 1.1em;
            margin-bottom: 20px;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            transition: transform 0.3s ease;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .stat-icon {
            font-size: 2.5em;
            margin-bottom: 15px;
        }
        
        .stat-number {
            font-size: 2.2em;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 5px;
        }
        
        .stat-label {
            color: #666;
            font-size: 1em;
        }
        
        .content-grid {
            display: grid;
            grid-template-columns: 2fr 1fr;
            gap: 30px;
        }
        
        .content-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
        }
        
        .card-title {
            font-size: 1.3em;
            color: #333;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .todo-item, .doc-item {
            padding: 15px 0;
            border-bottom: 1px solid #f0f2f5;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .todo-item:last-child, .doc-item:last-child {
            border-bottom: none;
        }
        
        .priority-high {
            color: #e74c3c;
        }
        
        .priority-medium {
            color: #f39c12;
        }
        
        .priority-low {
            color: #27ae60;
        }
        
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-size: 0.9em;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background: #667eea;
            color: white;
        }
        
        .btn-primary:hover {
            background: #5a67d8;
            transform: translateY(-1px);
        }
        
        .btn-outline {
            background: transparent;
            border: 1px solid #667eea;
            color: #667eea;
        }
        
        .btn-outline:hover {
            background: #667eea;
            color: white;
        }
        
        .logout-btn {
            background: rgba(255,255,255,0.2);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
            padding: 8px 16px;
            border-radius: 6px;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        .logout-btn:hover {
            background: rgba(255,255,255,0.3);
            color: white;
        }
        
        .loading {
            text-align: center;
            padding: 20px;
            color: #666;
        }
        
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 15px;
            }
            
            .nav-content {
                flex-direction: column;
                gap: 10px;
            }
            
            .content-grid {
                grid-template-columns: 1fr;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <!-- 头部 -->
    <header class="header">
        <div class="header-content">
            <div class="logo">
                <span class="logo-icon">🏢</span>
                <span>OA 办公自动化系统</span>
            </div>
            <div class="user-info">
                <div class="user-avatar">
                    <span th:text="${#strings.substring(fullName != null ? fullName : username, 0, 1)}">U</span>
                </div>
                <div>
                    <div th:text="${fullName != null ? fullName : username}">用户名</div>
                    <div style="font-size: 0.9em; opacity: 0.8;" th:text="${email}">email@example.com</div>
                </div>
                <a href="/logout" class="logout-btn">退出登录</a>
            </div>
        </div>
    </header>
    
    <!-- 导航菜单 -->
    <nav class="nav-menu">
        <div class="nav-content">
            <a href="/dashboard" class="nav-item active">仪表板</a>
            <a href="/documents" class="nav-item" sec:authorize="hasAuthority('SCOPE_read') or hasAuthority('oa:read')">文档管理</a>
            <a href="/workflows" class="nav-item" sec:authorize="hasAuthority('SCOPE_write') or hasAuthority('oa:write')">流程管理</a>
            <a href="/admin" class="nav-item" sec:authorize="hasAuthority('ROLE_ADMIN') or hasAuthority('oa:all')">系统管理</a>
            <a href="http://www.oauth2server.com:8081" class="nav-item" target="_blank">风控系统</a>
        </div>
    </nav>
    
    <!-- 主要内容 -->
    <main class="main-content">
        <!-- 欢迎区域 -->
        <section class="welcome-section">
            <h1 class="welcome-title">欢迎回来，<span th:text="${fullName != null ? fullName : username}">用户</span>！</h1>
            <p class="welcome-subtitle">今天是个美好的工作日，让我们开始高效的办公吧！</p>
            <div style="display: flex; gap: 15px; margin-top: 20px;">
                <a href="/documents/create" class="btn btn-primary" sec:authorize="hasAuthority('SCOPE_write') or hasAuthority('oa:write')">创建文档</a>
                <a href="/workflows" class="btn btn-outline">查看流程</a>
            </div>
        </section>
        
        <!-- 统计卡片 -->
        <section class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon">📄</div>
                <div class="stat-number" id="docCount">-</div>
                <div class="stat-label">我的文档</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">📋</div>
                <div class="stat-number" id="todoCount">-</div>
                <div class="stat-label">待办事项</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">✅</div>
                <div class="stat-number">12</div>
                <div class="stat-label">已完成任务</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">📊</div>
                <div class="stat-number">85%</div>
                <div class="stat-label">工作效率</div>
            </div>
        </section>
        
        <!-- 内容区域 -->
        <section class="content-grid">
            <!-- 待办事项 -->
            <div class="content-card">
                <h2 class="card-title">
                    <span>📋</span>
                    <span>待办事项</span>
                </h2>
                <div id="todoList" class="loading">正在加载...</div>
            </div>
            
            <!-- 最近文档 -->
            <div class="content-card">
                <h2 class="card-title">
                    <span>📄</span>
                    <span>最近文档</span>
                </h2>
                <div id="docList" class="loading">正在加载...</div>
            </div>
        </section>
    </main>
    
    <script>
        // 加载待办事项
        async function loadTodos() {
            try {
                const response = await fetch('/api/todos');
                const result = await response.json();
                
                if (result.code === 200) {
                    const todos = result.data.todos;
                    document.getElementById('todoCount').textContent = todos.length;
                    
                    const todoList = document.getElementById('todoList');
                    todoList.innerHTML = todos.map(todo => `
                        <div class="todo-item">
                            <div>
                                <div style="font-weight: 500;">${todo.title}</div>
                                <div style="font-size: 0.9em; color: #666; margin-top: 5px;">
                                    截止时间: ${new Date(todo.dueDate).toLocaleString()}
                                </div>
                            </div>
                            <div class="priority-${todo.priority === '高' ? 'high' : todo.priority === '中' ? 'medium' : 'low'}">
                                ${todo.priority}
                            </div>
                        </div>
                    `).join('');
                } else {
                    document.getElementById('todoList').innerHTML = '<div style="color: #e74c3c;">加载失败</div>';
                }
            } catch (error) {
                console.error('加载待办事项失败:', error);
                document.getElementById('todoList').innerHTML = '<div style="color: #e74c3c;">加载失败</div>';
            }
        }
        
        // 加载文档列表
        async function loadDocuments() {
            try {
                const response = await fetch('/api/documents');
                const result = await response.json();
                
                if (result.code === 200) {
                    const docs = result.data.documents;
                    document.getElementById('docCount').textContent = docs.length;
                    
                    const docList = document.getElementById('docList');
                    docList.innerHTML = docs.map(doc => `
                        <div class="doc-item">
                            <div>
                                <div style="font-weight: 500;">${doc.title}</div>
                                <div style="font-size: 0.9em; color: #666; margin-top: 5px;">
                                    作者: ${doc.author} | ${new Date(doc.createTime).toLocaleDateString()}
                                </div>
                            </div>
                            <a href="#" class="btn btn-outline">查看</a>
                        </div>
                    `).join('');
                } else {
                    document.getElementById('docList').innerHTML = '<div style="color: #e74c3c;">加载失败</div>';
                }
            } catch (error) {
                console.error('加载文档列表失败:', error);
                document.getElementById('docList').innerHTML = '<div style="color: #e74c3c;">加载失败</div>';
            }
        }
        
        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            loadTodos();
            loadDocuments();
            
            // 每30秒刷新一次数据
            setInterval(() => {
                loadTodos();
                loadDocuments();
            }, 30000);
        });
        
        // 导航菜单激活状态
        document.querySelectorAll('.nav-item').forEach(item => {
            if (item.href === window.location.href) {
                item.classList.add('active');
            }
        });
    </script>
</body>
</html> 